# -*- coding: utf-8 -*-

# Process 数据处理

from morsecoder.error import *
from morsecoder.constant import *
from morsecoder.convert import *

# Encode
def get_encode(string: str, **option) -> str:
    string = string.upper().replace(' ', '')
    long, short = option_convert(option, is_encode=True).values()

    for value in string:
        try:
            yield encode_convert(encode_list[value], long=long, short=short)

        except KeyError:
            yield encode_convert(bin(ord(value))[2:], long=long, short=short)

# Decode
def get_decode(string: str, **option) -> str:
    sep, long, short = option_convert(option, is_encode=False).values()
    code = string.split(sep)

    if not code[-1]:
        code.pop()

    for value in code:
        try:
            yield decode_list[decode_convert(value, long=long, short=short)]

        except KeyError: # Special character
            yield chr(int(decode_convert(value, long=long, short=short), 2))
